#!/bin/bash

# qt-proxy sysVinit config file

. /etc/init.d/functions

# Path to the qt proxy config file.
CONFIG_FILE_PATH="/etc/qingtian/enclave/qt-proxy.conf"

# Path to the qt proxy log file.
LOG_FILE_PATH="/var/log/qt_proxy.log"

# Config variables to be populated when parsing the config file.
declare -A CONFIG
CONFIG[local_port]=
CONFIG[remote_addr]=
CONFIG[remote_port]=
CONFIG[ipv6]=

function parse_config {
    local count=0
    while read line; do
        count=$((count+1))
        # Skip comment lines and blank lines
        echo "$line" | grep -Eq "^$|^#" && continue
        local key
        key=$(echo "$line" | cut -d: -f1 | egrep -io "^([-_a-z0-9])+")
        [[ -z ${CONFIG[$key]+x} ]] && echo "Error in $CONFIG_FILE_PATH:$count - unexpected: $key"
        local value
        value=$(echo "$line" | sed -E "s/^.+://" | sed -E "s/\\s+\"?//" | sed -E "s/\"?\$//")
        [[ -z $value ]] && echo "Error in $CONFIG_FILE_PATH:$count - missing value for $key"
        CONFIG[$key]="$value"
        echo "CONFIG[$key]=$value"
    done < "$CONFIG_FILE_PATH"
    # Some trivial config validation.
    [[ -z ${CONFIG[local_port]} ]] && \
        echo "Config error: missing local vsock port (\`local_port\`)." && exit 1
    [[ -z ${CONFIG[remote_addr]} ]] && \
        echo "Config error: missing remote address (\`remote_addr\`)." && exit 1
    [[ -z ${CONFIG[remote_port]} ]] && \
        echo "Config error: missing remote port (\`remote_port\`)." && exit 1
    [[ -z ${CONFIG[ipv6]} ]] && \
        echo "Config error: missing ipv6 support (\`ipv6\`)." && exit 1
    echo "local_port = ${CONFIG[local_port]}, remote_addr = ${CONFIG[remote_addr]},"\
    "remote_port = ${CONFIG[remote_port]}, ipv6 = ${CONFIG[ipv6]}"
}

start() {
    parse_config
    proxy_pid="$(pidof qt_proxy)"
    if [ ${proxy_pid} ]; then
        echo "The qt-proxy process is existed, please clear ..."
        exit 1
    fi
    echo "Starting qt-proxy service ..."
    if [ ${CONFIG[ipv6]} == 1 ]; then
        /usr/local/bin/qingtian/enclave/qt_proxy -l ${CONFIG[local_port]} -a ${CONFIG[remote_addr]} -p ${CONFIG[remote_port]} -6 &
    elif [ ${CONFIG[ipv6]} == 0 ]; then
        /usr/local/bin/qingtian/enclave/qt_proxy -l ${CONFIG[local_port]} -a ${CONFIG[remote_addr]} -p ${CONFIG[remote_port]} &
    else
        echo "invalid ipv6 value: ${CONFIG[ipv6]}, only support 0 or 1"
        exit 1
    fi
    proxy_pid="$(pidof qt_proxy)"
    echo "proxy pid = [$proxy_pid]"
    if [ ! ${proxy_pid} ]; then
        sleep 1
        echo "try to get pid again ...."
        proxy_pid="$(pidof qt_proxy)"
        echo "[try value]proxy pid = [$proxy_pid]"
        if [ ! ${proxy_pid} ]; then
            echo "The qt-proxy service start fail, please check ${LOG_FILE_PATH}"
            exit 1
        fi
    fi
    touch /var/lock/subsys/qt-proxy
}

stop() {
    echo "Stopping qt-proxy service ..."
    killproc qt_proxy
    rm -f /var/lock/subsys/qt-proxy
}

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|reload|condrestart)
        stop
        start
        ;;
    status)
        status qt-proxy
        ;;
    *)
        echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
        exit 1
esac

exit 0
